//
//  YCSettingViewController.m
//  YCVillageDoctor
//
//  Created by zhangxiaoliang on 2017/6/8.
//  Copyright © 2017年 zhangxiaoliang. All rights reserved.
//

#import "YCSettingViewController.h"
#import "ExpertInformationViewCell.h"
#import "LoginViewController.h"
#import "SDImageCache.h"
#import "OpinionViewController.h"
#import "YCNavViewController.h"
#import "YCSSKeychain.h"
#import "UIColor+Extension.h"


@interface YCSettingViewController ()
@property (nonatomic,strong)NSArray *dataSource;

@property (nonatomic ,assign)CGFloat cacheSize;
@property (nonatomic ,strong)UIPickerView *pickerView;

@property (nonatomic ,strong)NSArray *modeldataSoure;

@property(nonatomic,strong)NSString * modelText;

@property(nonatomic,strong)UIView* viewBG;

@property(nonatomic,strong)UIView* grayPickerView;

//按钮开关
@property(nonatomic,strong)UIButton* btn;

@end

@implementation YCSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self stupUI];
}


- (void)stupUI
{
    //    1. 设置navigation
    self.title = @"设置";
    self.view.backgroundColor = [UIColor whiteColor];
    //    2. 设置tableView
    [self.tableView registerNib:[UINib nibWithNibName:@"ExpertInformationViewCell"  bundle:nil] forCellReuseIdentifier:@"ExpertInformationViewCell"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;

    [self.tableView reloadData];

    //    3. 设置的名称列表
    _dataSource = @[ @"清除缓存", @"意见反馈"];
}

-(UIView*)viewBG
{
    if (!_viewBG) {
        _viewBG=[[UIView alloc] initWithFrame:self.view.frame];
        [self.view addSubview:_viewBG];
        UITapGestureRecognizer* tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
        [_viewBG addGestureRecognizer:tap];
    }
    return _viewBG;
}
#pragma mark - Table view data source
#pragma mark 总共多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.dataSource.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {

    return 144;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {

    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 144)];
    view.backgroundColor = [UIColor clearColor];

    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(12, 50, kScreenWidth - 24, 44);
    button.titleLabel.font = BigTitleFont;
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setBackgroundColor:[UIColor colorWithRGBValue:0x0092F0]];
    button.layer.cornerRadius = 3;
    button.clipsToBounds = YES;
    [button setTitle:@"退出登录" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(logout) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:button];

    return  view;
}
#pragma mark 每一行的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.row == 0) {
        return 55;
    }
    return 45;
}


#pragma mark 每一行的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    ExpertInformationViewCell *cell = [ExpertInformationViewCell CellWithtable:tableView];

    cell.selectionStyle = UITableViewCellSelectionStyleNone;

    if (indexPath.row==0) {

        cell.arrowImage.hidden = YES;
        cell.rigitPaping.constant= 12;
        cell.subTitleLable.text =[NSString stringWithFormat:@"%0.1lfMB",[self ObtainCaches]];

    }

    cell.titleLable.text = self.dataSource[indexPath.row];
    return cell;
}
/*
 *获取缓存
 */
-(CGFloat)ObtainCaches
{

    NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];

    return [caches fileSize]/1000.0/1000.0;


}
#pragma mark 选中当前行的操作
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {


    if (indexPath.row == 0) {

        NSFileManager *fileManager=[NSFileManager defaultManager];
        NSString *caches = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
        if ([fileManager fileExistsAtPath:caches]) {
            NSArray *childerFiles = [fileManager subpathsAtPath:caches];
            for (NSString *fileName in childerFiles) {
                //如有需要，加入条件，过滤掉不想删除的文件
                NSString *absolutePath=[caches stringByAppendingPathComponent:fileName];
                [fileManager removeItemAtPath:absolutePath error:nil];
            }
        }
        [[SDImageCache sharedImageCache] clearDisk];

        [self presentSuccessTips:@"清除成功!"];

        NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];
        [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];

    }else if (indexPath.row == 1) {

        OpinionViewController *opinions = [[OpinionViewController alloc] init];
        [self.navigationController pushViewController:opinions animated:YES];

    }
}
-(void)tapClick:(UITapGestureRecognizer*)tap
{

    [UIView animateWithDuration:0.25 animations:^{
        self.grayPickerView.y=SCREEN_HEIGHT;
    } completion:^(BOOL finished) {

        [self.view sendSubviewToBack:self.viewBG];

    }];
}

#pragma mark - 退出登录
- (void)logout {

    UIAlertController *alertVc = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定要退出吗？" preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *alertActionC = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
    }];
    UIAlertAction *alertActionD = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        [YCSSKeychain saveValue:@"**" andKey:[YCUserTool sharedTools].linkMobile];

        [[NSUserDefaults standardUserDefaults] removeObjectForKey:KEXPERTID];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:KLINKMOBILE];
        LoginViewController *login = [[LoginViewController alloc] init];
        YCNavViewController * nav = [[YCNavViewController alloc] initWithRootViewController:login];
        window.rootViewController = nav;
    }];

    [alertVc addAction:alertActionD];
    [alertVc addAction:alertActionC];
    [self presentViewController:alertVc animated:YES completion:^{

    }];
}

@end
